#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#


from common.const import ExecuteResultEnum
from common.schemas.thrift_base_data_type import Application, ActionResult, ApplicationResource, JobPermission
from saporacle.comm.logger import logger
from saporacle.comm.saporacle_exception import SAPOracleException
from saporacle.resource.saporacle import SAPOracle


class SAPOracleResourceService:

    def __init__(self, pid: str, saporacle: SAPOracle) -> None:
        self._pid = pid
        self._saporacle = saporacle

    @staticmethod
    def query_job_permission(application: Application) -> JobPermission:
        permission = JobPermission()
        return permission

    def check_application(self, application: Application) -> ActionResult:
        try:
            self._saporacle.all_brtools_exist()
            not self._saporacle.is_backint_installed() and self._saporacle.install_backint()
            self._saporacle.login()
            self._saporacle.user_has_sufficient_privilege()
            _ = self._saporacle.sap_environments()
        except SAPOracleException as e:
            logger.error(f"Check application failed. {e}")
            return ActionResult(code=ExecuteResultEnum.INTERNAL_ERROR, bodyErr=e.err_code, bodyErrParams=e.parameters)
        logger.info("Check application succeeded.")
        return ActionResult(code=ExecuteResultEnum.SUCCESS)

    def query_cluster(self, application: Application) -> ApplicationResource:
        try:
            resource = ApplicationResource(extendInfo=self._saporacle.system_information())
        except SAPOracleException as e:
            logger.error(f"Query cluster failed. {e}")
            resource = ApplicationResource()
        logger.info("Query cluster succeeded.")
        return resource

